package com.zonta.finance.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zonta.finance.common.exception.BizException;
import com.zonta.finance.common.service.TokenService;
import com.zonta.finance.domain.dto.AdminDTO;
import com.zonta.finance.domain.form.RoleBindResourceForm;
import com.zonta.finance.domain.po.SysRoleBindResource;
import com.zonta.finance.mapper.SysRoleBindResourceMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zonta.finance.serivce.ISysRoleBindResourceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统角色绑定资源资源 服务实现类
 * </p>
 *
 * @author zonta
 * @since 2024-07-27
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SysRoleBindResourceServiceImpl extends ServiceImpl<SysRoleBindResourceMapper, SysRoleBindResource> implements ISysRoleBindResourceService {
    private final TokenService<AdminDTO> tokenService;
    private final TransactionTemplate transactionTemplate;
    @Override
        public List<Integer> listBindResourceIdByRoleId(Integer rid) {
        LambdaQueryWrapper<SysRoleBindResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleBindResource::getSysRoleId,rid);
        List<SysRoleBindResource> sysRoleBindResourceList = list(wrapper);
        if (CollectionUtils.isEmpty(sysRoleBindResourceList)) {
            return null;
        }
        return sysRoleBindResourceList.stream().map(SysRoleBindResource::getSysResourceId)
                .collect(Collectors.toList());
    }


    @Override
    public boolean roleBindResource(RoleBindResourceForm form) {

        if (CollectionUtils.isEmpty(form.getBindResourceIds()) && CollectionUtils.isEmpty(form.getUnBindResourceIds())) {
            throw new BizException("您未做任何更改，不能提交绑定");
        }
        List<SysRoleBindResource> sysRoleBindResourceList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(form.getBindResourceIds())) {
            //检测角色是否已经绑定过资源
            checkRoleBindResourceIds(form.getRoleId(), form.getBindResourceIds());
            for (Integer resourceId : form.getBindResourceIds()) {
                SysRoleBindResource sysRoleBindResource = new SysRoleBindResource();
                sysRoleBindResource.setSysRoleId(form.getRoleId());
                sysRoleBindResource.setSysResourceId(resourceId);
                sysRoleBindResource.setMemberId(tokenService.getThreadLocalUserId());
                sysRoleBindResource.setUpdateMemberId(tokenService.getThreadLocalUserId());
                sysRoleBindResource.initDefault();
                sysRoleBindResourceList.add(sysRoleBindResource);
            }
        }
        transactionTemplate.execute((transactionStatus) -> {
            if (!CollectionUtils.isEmpty(form.getBindResourceIds())) {
                return saveBatch(sysRoleBindResourceList);
            }
            if (!CollectionUtils.isEmpty(form.getUnBindResourceIds())) {
                LambdaQueryWrapper<SysRoleBindResource> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(SysRoleBindResource::getSysRoleId,form.getRoleId());
                wrapper.in(SysRoleBindResource::getSysResourceId,form.getUnBindResourceIds());
                int updateRows = baseMapper.delete(wrapper);
                if (updateRows != form.getUnBindResourceIds().size()) {
                    throw new BizException("解绑失败");
                }
            }
            return true;
        });
        return true;
    }
    private void checkRoleBindResourceIds(int roleId, List<Integer> resourceIds) {
        LambdaQueryWrapper<SysRoleBindResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleBindResource::getSysRoleId,roleId);
        wrapper.in(SysRoleBindResource::getSysResourceId,resourceIds);
        if (count(wrapper)>0){
            throw new BizException("角色不要重复绑定资源");
        }
    }
}
